package LetCode;

/**
 * @author: lwt
 * @date: 2025/7/31 8:52
 * @description:
 */
public class day7 {
    public static void main(String[] args) {
//        int[] prices = {7,1,5,3,6,4};
          int[] prices = {1,2,3,4,5};
//        int[] prices ={6,1,3,2,4,7};
        int result = maxProfit(prices);
        System.out.println(result);
    }

    public static int maxProfit(int[] prices) {
        int i = 0;
        int j = i+2;
        int n =prices.length;
        int profit = 0;
        int totalProfit = 0;
        while(j<n){
           if (prices[i]>prices[j] && prices[j-1] < prices[j]){
                profit =  prices[j] - prices[j-1];
                i+=3;
                j=i+2;
           } else if (prices[i] < prices[j] && prices[i] < prices[j-1]){
               profit = Math.max(prices[j] - prices[i], Math.max(prices[j] - prices[j-1],prices[j-1] -  prices[i]));
               i+=3;
               j=i+2;
           } else {
               profit = 0;
           }
           totalProfit += profit;
        }
        return totalProfit;
    }
}
